package com.cn.kehong.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ReadTextToExcel {

//    public static void main(String args[])throws Exception{
//        File file = new File("D:\\new3.txt");
//        try {
//            Scanner scanner = new Scanner(file);
//            Map<String, List<JSONObject>> maps = Maps.newLinkedHashMap();
//            String key = "";
//            String key2 = "";
//            while (scanner.hasNextLine()) {
//                String line = scanner.nextLine();
//                if(StringUtils.isEmpty(line)){
//                    continue;
//                }
//                if(line.indexOf(".cpp:") > 0){
//                    int index = line.lastIndexOf("\\");
//                    String path = line.substring(0,index+1);
//                    String fileName = line.substring(index+1,line.length() -1);
//                    key = path;
//                    key2 = fileName;
//                    if(maps.containsKey(path)){
//                        List<JSONObject> jsonObjects = maps.get(path);
//                        JSONObject object = new JSONObject();
//                        object.put("name",fileName);
//                        object.put("count",0);
//                        jsonObjects.add(object);
//                    }else{
//                        List<JSONObject> jsonObjects = Lists.newLinkedList();
//                        JSONObject object = new JSONObject();
//                        object.put("name",fileName);
//                        object.put("count",0);
//                        jsonObjects.add(object);
//                        maps.put(path,jsonObjects);
//                    }
//                }
//
//                if(line.indexOf("strcpy") > -1 && line.indexOf("//") < 0){
//                    List<JSONObject> jsonObjects = maps.get(key);
//                    for(int i =0 ;i < jsonObjects.size();i++){
//                        if(StringUtils.equals(jsonObjects.get(i).get("name").toString(),key2)){
//                            int count = (int) jsonObjects.get(i).get("count");
//                            count +=1;
//                            jsonObjects.get(i).put("count",count);
//                        }
//                    }
//                }
//            }
//            scanner.close();
//            Workbook workbook = new XSSFWorkbook();
//            Sheet sheet = workbook.createSheet("ExampleSheet");
//            int rowIndex = 0;
//            for(String mkey:maps.keySet()){
//                List<JSONObject> jsonObjects = maps.get(mkey);
//                for(int i =0 ;i < jsonObjects.size();i++){
//                    String name = jsonObjects.get(i).get("name").toString();
//                    int count = (int) jsonObjects.get(i).get("count");
//                    if(count == 0){
//                        continue;
//                    }
//                    Row row = sheet.createRow(rowIndex);
//                    Cell cell = row.createCell(0);
//                    cell.setCellValue("sys_admin\\"+mkey);
//                    cell = row.createCell(1);
//                    cell.setCellValue(name);
//                    cell = row.createCell(2);
//                    cell.setCellValue(count);
//                    System.out.println(mkey+name+":"+count);
//                    ++rowIndex;
//                }
//            }
//            FileOutputStream outputStream = new FileOutputStream("D:\\example.xlsx");
//            workbook.write(outputStream);
//        } catch (FileNotFoundException e) {
//            System.out.println("文件未找到");
//        }
//    }

    public static void main(String args[])throws Exception{
        File file = new File("D:\\text1.txt");
        try {
            Scanner scanner = new Scanner(file);
            Map<String, String> maps = Maps.newLinkedHashMap();
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if(StringUtils.isEmpty(line)){
                    continue;
                }

                if(line.indexOf("平台管理-系统管理") > 0||line.indexOf("平台管理-集群管理") > 0||line.indexOf("平台管理-多活管理") > 0||line.indexOf("平台管理-版本管理") > 0){
                    String[] words = line.split("\\s+");
                    int index = words[0].lastIndexOf("/");
                    String processName = words[0].substring(index+1);
                    String type = words[3];
                    maps.put(processName,type);
                    System.out.println(processName);
                    System.out.println(type);
                }
            }
            // 创建输入流
            BufferedReader reader = new BufferedReader(new FileReader("D:\\didiao1.txt"));

            // 创建临时文件
            File tempFile = new File("D:\\didiao11.txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

            // 逐行读取并修改内容
            String line;
            while ((line = reader.readLine()) != null) {
                String[] words = line.split("\\s+");
                int index = words[0].lastIndexOf("/");
                String processName = words[0].substring(index+1);
                if(maps.containsKey(processName)){
                    writer.write(maps.get(processName) + "\n"); // 写入新内容
                }else{
                    writer.write("1" + "\n"); // 保持原内容
                }
            }

            // 关闭输入输出流
            reader.close();
            writer.close();

        } catch (FileNotFoundException e) {
            System.out.println("文件未找到");
        }
    }
}
